﻿// Shader-Based-Image-Processing (SBIP)
// http://code.google.com/p/sbip/
//
// Copyright © Frank Nagl, 2009-2010
// admin@franknagl.de
//
namespace SBIP.Filter
{
    using System;
    using Helper;

    /// <summary>
    /// Extract RGB channel from image.
    /// </summary>
    /// 
    /// <remarks><para>Extracts specified channel of color image and returns
    /// it as grayscale image.</para>
    /// 
    /// <para>Sample usage:</para>
    /// <code>
    /// Bitmap image = new Bitmap("cathedral.jpg");
    ///
    /// // 1. Offline rendering of images
    /// // create Processor, used as rendering framework
    /// Processor processor = new Processor( );
    /// // starts Processor
    /// processor.Begin( image );
    /// // create ExtractChannel filter
    /// ExtractChannel filter = new ExtractChannel( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// filter.Channel = RGB.R;
    /// // apply the filter, render to bitmap  
    /// Bitmap resultImage = processor.RenderToBitmap( );
    /// resultImage.Save( "ExtractChannel.jpg", ImageFormat.Jpeg );
    /// // apply the filter, render to texture
    /// // Texture2D resultTexture = processor.RenderToTexture( );
    /// processor.End( );
    ///  
    /// // 2. Online rendering of videos or images
    /// // create any windows control for rendering in
    /// Form myForm = new Form
    /// {
    ///     Width = 1280,
    ///     Height = 720
    /// };
    /// // starts Processor
    /// processor.Begin( "Video.avi", myForm );
    /// // create ExtractChannel filter
    /// filter = new ExtractChannel( );
    /// processor.Filter = filter;
    /// // optional: configure filter
    /// filter.Channel = RGB.B;
    /// // apply the filter
    /// myForm.Show( );
    /// while ( myForm.Created )
    /// {
    ///     processor.Render( );
    ///     Application.DoEvents( );
    /// }
    /// processor.End( );
    /// </code>
    /// 
    /// <para><b>Initial image:</b></para>
    /// <img src="../../Cathedral.jpg" width="500" height="338" />
    /// <para><b>Result image:</b></para>
    /// <img src="../../ExtractChannel.jpg" width="500" height="338" />
    /// </remarks>
    public sealed class ExtractChannel : BaseFilter
    {
        private byte channel;       
        /// <summary>RGB channel to replace.</summary>
        /// <remarks>
        /// <para>Default value is set to <see cref="RGBA.B"/>.</para>
        /// </remarks>
        /// <exception cref="ArgumentException">Invalid channel is specified.
        /// </exception>
        public byte Channel
        {
            get { return channel; }
            set
            {
                if (value != RGBA.R && value != RGBA.G && value != RGBA.B)
                    throw new ArgumentException("Invalid channel is specified.");

                if (Effect == null)
                    throw new ArgumentException("Channel" + InitMsg);

                channel = value;
                Effect.Parameters["Channel"].SetValue(channel);
            }
        }     
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ReplaceChannel"/> class.
        /// </summary>
        public ExtractChannel()
            : base("ExtractChannel") { }
    }
}
